/*!
 @header    UnitView.m
 @abstract  添加成员animationView
 @author    丁磊
 @version   1.0.0 2014/05/28 Creation
 */

#import "UnitView.h"
#import "UnitCell.h"

#define defaultWidth  40      // 每一个unitCell的默认宽度
#define defaultPace   8       // unitCell之间的间距
#define duration      0     // 动画执行时间
#define defaultVisibleCount 4 //默认显示的unitCell的个数


@interface UnitView ()<UnitCellDelegate, UIScrollViewDelegate>
{
    int extendButton ;
}
@end

@implementation UnitView


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        extendButton = 0;
        [self setProperty];
    }
    _isDeleteable = YES;
    return self;
}
/*
 *   @method
 *   @function
 *   初始化_scrollView等
 */
- (void) setProperty
{
    _unitList = [[NSMutableArray alloc] init];
    _hasDelete = NO;
    _moveCount = 0;
    float fDoneWidth=0;
    if(_isDoneButton)
    {
        fDoneWidth =50;
        UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - 50, 0.5, fDoneWidth, self.bounds.size.height-1)];
        rightButton.backgroundColor = BASIC_BLUE_COLOR;
        rightButton.alpha = 0.5;
        [rightButton setTitle:@"完成" forState:UIControlStateNormal];
        rightButton.tag = DONETAG;
        [rightButton addTarget:self action:@selector(unitDoneTouched:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:rightButton];
    }

    UILabel *topline = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 0.5)];
    topline.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:topline];
    
    UILabel *bottomline = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.origin.y+self.bounds.size.height, self.bounds.size.width, 0.5)];
    bottomline.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:bottomline];


    _scrollView = [[UIScrollView alloc] init];
    _scrollView.delegate = self;
    _scrollView.frame = CGRectMake(0, (self.bounds.size.height - defaultWidth)/2.0, self.bounds.size.width -fDoneWidth, defaultWidth);
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.scrollEnabled = YES;
    _scrollView.directionalLockEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.alwaysBounceHorizontal = YES;
    _scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    _scrollView.contentSize = [self contentSizeForUIScrollView:0];
    [self addSubview:_scrollView];
    
    



    
/*
    _defaultUnit = [[UnitCell alloc] initWithFrame:CGRectMake(defaultPace, 0, defaultWidth, defaultWidth) andIcon:@"add" andName:@""];
    [_scrollView addSubview:_defaultUnit];
     _defaultUnit.delegate = self;
*/
    
    if(_isAddButton)
    {
        [self addNewUnit:@"addmore" withName:@"add" type:ADDTAG];
        extendButton++;
    }
    if(_isDelButton)
    {
        [self addNewUnit:@"del" withName:@"add" type:DELTAG];
        extendButton++;
    }
    [self scrollViewAbleScroll];
}

/*
 *  @method
 *  @function
 *  根据index获取UIScrollView的ContentSize
 */
- (CGSize)contentSizeForUIScrollView:(int)index
{
    float width = (defaultPace + defaultWidth) * index;
    if (width < _scrollView.bounds.size.width)
        width = _scrollView.bounds.size.width;
    return CGSizeMake(width, defaultWidth);
}

/*
 *  @method
 *  @function
 *  根据_unitList.count
 *  设置scrollView是否可以滚动
 *  设置scrollView的ContentSize
 *  设置scrollView的VisibleRect
 */
- (void)scrollViewAbleScroll
{
    //    _scrollView.scrollEnabled = (((_unitList.count + 1) * (defaultPace + defaultWidth)) > _scrollView.frame.size.width) ? YES : NO;
    _scrollView.contentSize = [self contentSizeForUIScrollView:(_unitList.count + 1 + extendButton)];
    [_scrollView scrollRectToVisible:CGRectMake(_scrollView.contentSize.width - defaultWidth, 0, defaultWidth, self.frame.size.height) animated:YES];
}

/*
 *  @method
 *  @function
 *  新增一个unitCell
 *  _defaultUnit向后移动并伴随动画效果
 *  newUnitCell渐变显示
 */
- (void) addNewUnit:(NSString *)icon withName:(NSString *)name type:(NSInteger)type
{
    __block UnitCell *newUnitCell;
    CGFloat x = ((_unitList.count)) * (defaultPace + defaultWidth) + defaultPace;
    newUnitCell = [[UnitCell alloc] initWithFrame:CGRectMake(x, 0, defaultWidth, defaultWidth) andIcon:icon andName:name];
    newUnitCell.alpha = 0.1;
    newUnitCell.delegate = self;
    newUnitCell.tag = type;
    if(ADDTAG == type)
        _addUnit = newUnitCell;
    if(DELTAG == type)
        _delUnit = newUnitCell;
    
    if(ADDTAG != type && DELTAG != type)
    [_unitList addObject:newUnitCell];
    
    
    [_scrollView addSubview:newUnitCell];
    [self scrollViewAbleScroll];
    _defaultUnit.alpha = 0.5;
    
    if(newUnitCell != _addUnit)
    {
        CGRect rect = _addUnit.frame;
        rect.origin.x += (defaultPace + defaultWidth);
        _addUnit.frame = rect;
        _addUnit.alpha = 1.0;
        _addUnit.alpha = 0.8;
        _addUnit.alpha = 1.0;
    }
    
    if(newUnitCell != _delUnit)
    {
        CGRect rect = _delUnit.frame;
        rect.origin.x += (defaultPace + defaultWidth);
        _delUnit.frame = rect;
        _delUnit.alpha = 1.0;
        _delUnit.alpha = 0.8;
        _delUnit.alpha = 1.0;
    }

    
    //[UIView animateWithDuration:duration animations:^(){
        CGRect rect = _defaultUnit.frame;
        rect.origin.x += (defaultPace + defaultWidth);
        _defaultUnit.frame = rect;
        _defaultUnit.alpha = 1.0;
        newUnitCell.alpha = 0.8;

   // } completion:^(BOOL finished){
        newUnitCell.alpha = 1.0;

   // }];

}

- (void) addNewUnit:(Contact *)contact
{
    __block UnitCell *newUnitCell;
    CGFloat x = ((_unitList.count)) * (defaultPace + defaultWidth) + defaultPace;
    newUnitCell = [[UnitCell alloc] initWithFrame:CGRectMake(x, 0, defaultWidth, defaultWidth) andContact:contact];
    newUnitCell.alpha = 0.1;
    newUnitCell.delegate = self;
    //20150715
    if(_isDeleteable==NO)//不可以删除
        newUnitCell.tag = DELUNABLETAG;
        
    [_unitList addObject:newUnitCell];
    [_scrollView addSubview:newUnitCell];
    [self scrollViewAbleScroll];
    _defaultUnit.alpha = 0.5;
    
    if(newUnitCell != _addUnit)
    {
        CGRect rect = _addUnit.frame;
        rect.origin.x += (defaultPace + defaultWidth);
        _addUnit.frame = rect;
        _addUnit.alpha = 1.0;
        _addUnit.alpha = 0.8;
        _addUnit.alpha = 1.0;
    }
    
    if(newUnitCell != _delUnit)
    {
        CGRect rect = _delUnit.frame;
        rect.origin.x += (defaultPace + defaultWidth);
        _delUnit.frame = rect;
        _delUnit.alpha = 1.0;
        _delUnit.alpha = 0.8;
        _delUnit.alpha = 1.0;
    }
    
    [UIView animateWithDuration:duration animations:^(){
        CGRect rect = _defaultUnit.frame;
        rect.origin.x += (defaultPace + defaultWidth);
        _defaultUnit.frame = rect;
        _defaultUnit.alpha = 1.0;
        newUnitCell.alpha = 0.8;
        
    } completion:^(BOOL finished){
        newUnitCell.alpha = 1.0;
        
    }];
}

- (void) removeUnit:(Contact *)contact
{
    for(UnitCell * unitCell in _unitList)
    {
        if([unitCell.contact.mID isEqualToString:contact.mID])
        {
            [self ClearCell:unitCell];
            break;
        }
    }
}

- (void) removeAll
{
    //清理数据，由于清理时会对_unitList进行操作，不能从头开始遍历，需要从尾部开始遍历
    int count = _unitList.count;
    for( int i=count-1; i>=0; i--){
        UnitCell * unitCell  = [_unitList objectAtIndex:i];
        if(unitCell.tag == ADDTAG || unitCell.tag ==DELTAG)
        {
            continue;
        }
        [self ClearCell:unitCell];
    }
}



/*
 *  @method
 *  @function
 *  unitCell被点击代理，需要执行删除操作
 */
- (void)ClearCell:(UnitCell *)unitCell
{
    
    if(unitCell == _defaultUnit || unitCell.tag == ADDTAG || unitCell.tag == DELTAG)
    {
        return;
    }
    
    
    _hasDelete = YES;
    int index = (int)[_unitList indexOfObject:unitCell];
    
    // step_1: 设置相关unitCell的透明度
    unitCell.alpha = 0.8;
    
    // 判断其余cell的移动方向（从前向后移动/从后向前移动）
    _frontMove = NO;
    if (_unitList.count - 1 > defaultVisibleCount
        && (_unitList.count - index - 1) <= defaultVisibleCount) {
        _frontMove = YES;
    }
    if (index == _unitList.count - 1 && !_frontMove)
        _defaultUnit.alpha = 0.5;
    
    //[UIView animateWithDuration:duration animations:^(){
    
    // step_2: 其余unitCell依次移动
    if (_frontMove)
    {
        // 前面的向后移动
        for (int i = 0; i < index; i++) {
            UnitCell *cell = [_unitList objectAtIndex:(NSUInteger) i];
            CGRect rect = cell.frame;
            rect.origin.x += (defaultPace + defaultWidth);
            cell.frame = rect;
        }
        _moveCount++;
    }
    else
    {
        // 后面的向前移动
        for (int i = index + 1; i < _unitList.count; i++) {
            UnitCell *cell = [_unitList objectAtIndex:(NSUInteger)i];
            CGRect rect = cell.frame;
            rect.origin.x -= (defaultPace + defaultWidth);
            cell.frame = rect;
        }
        
        // step_3: _defaultUnit向前移动
        
        CGRect rect = _addUnit.frame;
        rect.origin.x -= (defaultPace + defaultWidth);
        _addUnit.frame = rect;
        _addUnit.alpha = 1.0;
        
        
        rect = _delUnit.frame;
        rect.origin.x -= (defaultPace + defaultWidth);
        _delUnit.frame = rect;
        _delUnit.alpha = 1.0;
        
        rect = _defaultUnit.frame;
        rect.origin.x -= (defaultPace + defaultWidth);
        _defaultUnit.frame = rect;
        _defaultUnit.alpha = 1.0;
        
    }
    unitCell.alpha = 0.0;
    
    // } completion:^(BOOL finished){
    
    // step_4: 删除被点击的unitCell
    [unitCell removeFromSuperview];
    [_unitList removeObject:unitCell];
    
    if (_unitList.count <= defaultVisibleCount)
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    if (_frontMove) {
        [self isNeedResetFrame];
    }
    //}];
    
    
}

/*
 *  @method
 *  @function
 *  unitCell被点击代理，需要执行删除操作
 */
- (void)unitCellTouched:(UnitCell *)unitCell
{
    if (_delegate && [_delegate respondsToSelector:@selector(extendCellTouched:)])
        [_delegate extendCellTouched:unitCell];
    
    if(unitCell == _defaultUnit || unitCell.tag == ADDTAG || unitCell.tag == DELTAG  || unitCell.tag == DELUNABLETAG)
    {
        return;
    }
    
    [self ClearCell:unitCell];

}

/*
 *  @method
 *  @function
 *  unitCell被点击代理，需要执行删除操作
 */
- (void)unitDoneTouched:(id)sender
{
    if (_delegate && [_delegate respondsToSelector:@selector(unitDoneTouched:)])
        [_delegate unitDoneTouched:sender];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
//    [self isNeedResetFrame];
}

/*
 *  @method
 *  @function
 *  当删除操作是前面的unitCell向后移动时
 *  做滚动操作或者添加操作需要重新设置每个unitCell的frame
 */
- (void)isNeedResetFrame
{
    if (_frontMove && _moveCount > 0) {
        
        for (int i = 0; i < _unitList.count; i++) {
            UnitCell *cell = [_unitList objectAtIndex:(NSUInteger) i];
            CGRect rect = cell.frame;
            rect.origin.x -= (defaultPace + defaultWidth) * _moveCount;
            cell.frame = rect;
        }
        
        CGRect rect = _defaultUnit.frame;
        rect.origin.x -= (defaultPace + defaultWidth) * _moveCount;
        _defaultUnit.frame = rect;
        //需要调整添加删除图标的位置
        rect = _addUnit.frame;
        rect.origin.x -= (defaultPace + defaultWidth);
        _addUnit.frame = rect;
        
        
        rect = _delUnit.frame;
        rect.origin.x -= (defaultPace + defaultWidth);
        _delUnit.frame = rect;
        
        
        _frontMove = NO;
        _moveCount = 0;
    }
    
    if (_hasDelete)
    {
        _scrollView.contentSize = [self contentSizeForUIScrollView:(_unitList.count + 1)];
        _hasDelete = !_hasDelete;
    }
}
@end
